package com.mh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mh.dao.AppointmentInfoDao;
import com.mh.pojo.AppointmentInfo;
import com.mh.service.AppointmentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * Date:2023/4/13
 * author:zmh
 * description: 预约信息类实现类
 **/

@Service
public class AppointmentInfoServiceImpl extends ServiceImpl<AppointmentInfoDao, AppointmentInfo> implements AppointmentInfoService {

    @Autowired
    private AppointmentInfoDao appointmentInfoDao;

    /**
     * 添加预约信息
     * @param appointmentInfo 预约信息对象
     * @return 返回执行状态
     */
    @Override
    public String addAppointmentInfo(AppointmentInfo appointmentInfo) {
        // 填充默认字段
        appointmentInfo.setStatus("0"); // 执行状态，默认待接诊
        appointmentInfo.setOperationDoctor(""); // 操作医生-默认无
        appointmentInfo.setUid(UUID.randomUUID().toString()); // uid，随机生成预约单号
        int insert = appointmentInfoDao.insert(appointmentInfo);
        return insert==1? "操作成功!" : "操作失败!";
    }

    /**
     * 获取所有预约信息-用户界面
     * @return 返回预约信息列表
     */
    @Override
    public List<AppointmentInfo> getALlByStudentPage() {
        //构造排序条件
        LambdaQueryWrapper<AppointmentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(AppointmentInfo::getAppointmentTime);
        queryWrapper.orderByDesc(AppointmentInfo::getStatus);

        List<AppointmentInfo> list = appointmentInfoDao.selectList(queryWrapper);
        list.forEach(item->{
            item.setAppointmentType(null);
            item.setGender(null);
            item.setContactWay(null);
            item.setOperationDoctor(null);

        });
        // 将状态为2和3的过滤掉，只剩下待接诊和正在接诊
        List<AppointmentInfo> finalList =  list.stream().map(item->{
            if (item.getStatus().equals("2") || item.getStatus().equals("3")){
                return null;
            }else {
                return item;
            }
        }).collect(Collectors.toList());
        // 移除所有为null的元素
        finalList.removeAll(Collections.singleton(null));
        return finalList;
    }

    /**
     * 医生界面-获取所有信息
     * @return 返回预约信息列表
     */
    @Override
    public List<AppointmentInfo> getAllDoctorPage() {
        //构造排序条件
        LambdaQueryWrapper<AppointmentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(AppointmentInfo::getAppointmentTime);
        queryWrapper.orderByDesc(AppointmentInfo::getStatus);
        // 执行查询
        List<AppointmentInfo> list = appointmentInfoDao.selectList(queryWrapper);
        // 将状态为2和3的过滤掉，只剩下待接诊和正在接诊
        List<AppointmentInfo> finalList =  list.stream().map(item->{
            if (item.getStatus().equals("2") || item.getStatus().equals("3")){
                return null;
            }else {
                return item;
            }
        }).collect(Collectors.toList());
        // 移除所有为null的元素
        finalList.removeAll(Collections.singleton(null));
        return finalList;
    }

    /**
     * 用户界面-获取单个学生的预约信息
     * @param studentName 学生姓名
     * @return 单个学生的预约信息
     */
    @Override
    public List<AppointmentInfo> getOneStudentAppointmentInfo(String studentName) {
        // 条件构造器
        LambdaQueryWrapper<AppointmentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppointmentInfo::getStudentName, studentName);
        // 查询单个学生预约信息
        List<AppointmentInfo> list = appointmentInfoDao.selectList(queryWrapper);
        return list;
    }

    /**
     * 医生界面-修改预约状态信息
     * @param status 【0-3】
     * @return 执行结果信息
     */
    @Override
    public String modifyAppointmentStatus(String uid,String status) {
        LambdaUpdateWrapper<AppointmentInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppointmentInfo::getUid,uid);
        updateWrapper.set(AppointmentInfo::getStatus,status);
        int update = appointmentInfoDao.update(null, updateWrapper);
        return update==1? "操作成功!" : "操作失败!";
    }

    /**
     * 修改预约订单的操作医生姓名
     * @param uid 预约单号
     * @param doctorName 医生名称
     * @return 执行状态结果
     */
    @Override
    public String modifyAppointmentOperationDoctor(String uid, String doctorName) {
        LambdaUpdateWrapper<AppointmentInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppointmentInfo::getUid,uid);
        updateWrapper.set(AppointmentInfo::getOperationDoctor, doctorName);
        int update = appointmentInfoDao.update(null, updateWrapper);
        return update==1? "操作成功!" : "操作失败!";
    }
}
